package cn.silver.framework.mybatis.bean;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 分页数据
 *
 * @author Silver Zhou
 */
@Data
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "PageDomain", description = "分页对象")
public class PageModel implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;


    /**
     * 当前记录起始索引
     */
    @Schema(title = "当前记录起始索引")
    private Integer pageNum;
    /**
     * 每页显示记录数
     */
    @Schema(title = "每页显示记录数")
    private Integer pageSize;
    /**
     * 排序列
     */
    @Schema(title = "排序列")
    private String orderByColumn;
    /**
     * 排序的方向desc或者asc
     */
    @Schema(title = "排序的方向")
    private String isAsc;
    /**
     * 分页参数合理化
     */
    private Boolean reasonable = true;

    public PageModel(Integer pageNum, Integer pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public Integer getTotalPage(Long total) {
        return BigDecimal.valueOf(total).divide(BigDecimal.valueOf(pageSize), 0, RoundingMode.UP).intValue();
    }
}
